# -*- coding:utf-8 -*-

"""
DESC:
auth:lt
date:2023-08-21
"""
import requests
import datetime
import random
import json

# 配置Token和Headers
token = """
your token
"""
token = token.strip()
headers = {
    "Authorization": "Basic Y2xpZW50OmFkbWlu",
    "Accesstoken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5mbyI6InNlY3VyaXR5OmF5RGRlTVR1UitPOGM2SWxiZ2REaDY2bGZEeUJoUzhOM0JTbENmUXJKSGpFTWhTVEFCVWZwZnoyUXVHNStUY2pUWXNZb1JRZnp6R05HUC9ZVVdKZ2hQNDFBZGdQZjFxUGhWM2lCdW1Wak9VVTNQbnBSSWxXTG8wZkROUDIwckVRU1h4VVBibkViSWhmUXFJTGticWViK0VpaE9JZ2FJNEtLaURRakt0QkdJaTBmTmZjb1g1Z3ZKZGNRUEd2U0E5Z1FsQWxqUEZTeXc3RlVSNk1TYzhWWTZLRXo5SnR3YUpmVERzaHh2aVNySmVtOS84QXhaV1IwVmQ5WldtVTY2aG9WTGs0LzVMVEV6TGxmZ21Qbzk3ejNqdlJiei9VVjlKendsbzBmODlaSHhocVU5aGRaM3JTTG4vVGsrY1p4TU1HK2dnb1F0dHZEOEo5MTNEOVB4NkhDY3cyVVRPdVdTQ2dtYUV3V3JUZEVZekcvK25xZ3BTdkZaSTYzV0dsa1hFOCIsInVzZXJQZXJtS2V5IjoibHVvd2VuamllQGNvb2NhYS5jb21fcGVybV9rZXkiLCJkZXBOYW1lIjoi6L2v5Lu2LeS4u-WKqOacjeWKoeaKgOacr-e7hCIsImNsaWVudF9pZCI6ImNsaWVudCIsInJvbGVJZHMiOm51bGwsInBpbiI6MywiZGVwVW5pcXVlSWQiOiIxNjg5NzEzOTMxMjU5NDc4MDE2Iiwic2NvcGUiOlsiYXBwIl0sIm5pY2tuYW1lIjoi572X5paH5rSBIiwidGVuYW50SWQiOiI2MzRkMWM4OWQ1NmQ0N2VkYTU2ODQ0ODBlYzg4OGUyMyIsImlkIjoxNDksImV4cCI6MTY5Mjk3Mjc3Nywicm9sZU5hbWVzIjpudWxsLCJ1bmlxdWVJZCI6IjE1MzQwNzA2OTAzMDU1NDAwOTYiLCJqdGkiOiIyNDljMWEyMS02YmQxLTRkNWYtOTJkZS0zZWM1YTg3NjM3MTciLCJ1c2VybmFtZSI6Imx1b3dlbmppZUBjb29jYWEuY29tIn0.fD__IBXI6lrJXtOlPWNNARS9mdwdbNvzupuf8Moea3eDB0ejEEc4kgg2kZUD4wDII6w1JvBmZ9zL9E1X5vqa4oy-C2ssZnQir1lTN3B668do-DOeGQqAEtbVTmLk8tftzbcOQ02gFT-mYzNoV4PYlYpyCwwx7QWbNaE4ScxSWNFZ5ynwIuiOhoHaGoW4RL6rigM4BbPzH7opkkctRpJ78Hdkc7sMcC3_aiAs0gzZDy25FbmeYry30qqaK9v41PnCC9IRLjxcPd-r56T5kOuB9ohSbbuC62IKxlYiOZSjGF4mpu316o_sBomuMMw5qitt8mLsyq8yiIrmirKeSU7ktg"
}

# API URL
api_url = "https://console.cloud.coocaa.com/violet-api/workHour/admin/projectMatterUserPlan/report/list"

# 获取API数据
try:
    api_response = requests.get(api_url, headers=headers)
    if api_response.status_code == 200:
        api_data = api_response.json()
        if api_data.get("result") != "success":
            print(f"API returned an unsuccessful result: {api_data.get('msg', 'Unknown error')}")
            exit(1)
    else:
        print(f"API returned a non-200 status code: {api_response.status_code}")
        exit(1)
except requests.RequestException as e:
    print(f"An error occurred while fetching API data: {e}")
    exit(1)
except json.JSONDecodeError as e:
    print(f"An error occurred while decoding API response: {e}")
    exit(1)

# 随机备注列表
remarks_list = ["开发功能", "编写代码", "设计软件"]

# 获取当前日期和本周一的日期
today = datetime.datetime.now()
monday = today - datetime.timedelta(days=today.weekday())

# 创建一个日期列表（从周一到周五）
dates = [monday + datetime.timedelta(days=i) for i in range(5)]

# 遍历日期和时间段
for date in dates:
    for time_range in [("10:00:00", "12:00:00"), ("13:30:00", "19:30:00")]:
        # 从API数据中随机选择一个项目
        selected_item = random.choice(api_data["data"])

        # 随机选择一个备注
        remarks = random.choice(remarks_list)

        # 创建数据字典
        data = {
            "matterCode":"1687018835229540355",
            "matterName": "个人-代码编写",
            "typeCode": selected_item["typeCode"],
            "typeName": selected_item["typeName"],
            "projectCode": selected_item["projectCode"],
            "projectName": selected_item["projectName"],
            "startFrom": f"{date.strftime('%Y-%m-%d')} {time_range[0]}",
            "endAt": f"{date.strftime('%Y-%m-%d')} {time_range[1]}",
            "fillDate": f"{date.strftime('%Y-%m-%d')}",
            "remarks": "生活助手V2.1版本开发"
        }

        print(data)

        url = "https://console.cloud.coocaa.com/violet-api/workHour/admin/workHour/report"
        # 发起POST请求（由于我的环境不能进行网络请求，您需要在您自己的环境中取消以下行的注释）
        response = requests.post(url, headers=headers, json=data)

        # # 打印响应和请求数据（用于调试）
        # print(response.status_code)
        # print(response.text)
        # print(f"Sent request for date: {date}, time range: {time_range}")
